package com.yang.mall.tools;

import com.yang.mall.exception.BaseException;
import com.yang.mall.exception.BaseExceptionDefine;
import net.sf.json.JSONObject;
import org.slf4j.LoggerFactory;

/**
 * Created by oracle on 17/1/18.
 */
public class ReturnMessagePackage {

    public static JSONObject returnSuccessJSONObject(){
        JSONObject jsonMapResult = new JSONObject();
        jsonMapResult.put("status", BaseKeyValueDefine.RETURN_PACKAGE_SUCCESS_CODE);
        jsonMapResult.put("message", BaseKeyValueDefine.RETURN_PACKAGE_SUCCESS_MESSAGE);

        return jsonMapResult;
    }

    public static JSONObject returnSuccess(JSONObject object, String message){
        JSONObject jsonMapResult = new JSONObject();
        jsonMapResult.put("resultCode", BaseKeyValueDefine.RETURN_CODE);
        jsonMapResult.put("status", BaseKeyValueDefine.RETURN_PACKAGE_SUCCESS_STATUS);
        jsonMapResult.put("message", message);
        jsonMapResult.put("date", object);
        return jsonMapResult;
    }


    public static String returnErrorMsg(Exception e){
        if(e == null){
            return  returnSystemBusyError();
        }

        JSONObject jsonMapResult = new JSONObject();
        jsonMapResult.put("code", 500);
        jsonMapResult.put("message", e.getMessage());

        String result = jsonMapResult.toString();

        LoggerFactory.getLogger(ReturnMessagePackage.class).info(result);
        return result;
    }


    public static String returnErrorMsg(BaseException e, String message){
        if(e == null){
            return  returnSystemBusyError();
        }

        JSONObject jsonMapResult = new JSONObject();
        jsonMapResult.put("code", String.valueOf(e.getReturnCode()));
        jsonMapResult.put("message", message);

        String result = jsonMapResult.toString();

        LoggerFactory.getLogger(ReturnMessagePackage.class).info(result);
        return result;
    }

    public static String returnSystemBusyError(){
        String result = "{\"code\": \"" + BaseExceptionDefine.SYSTEM_BUSY_CODE + "\",\"message\": \"" + BaseExceptionDefine.SYSTEM_BUSY_DESC + "\"}";
        LoggerFactory.getLogger(ReturnMessagePackage.class).info(result);
        return result;
    }

/*
    public static String returnMsg(){
        JSONObject jsonMapResult = new JSONObject();
        jsonMapResult.put("code", BaseKeyValueDefine.RETURN_PACKAGE_SUCCESS_CODE);
        jsonMapResult.put("message", BaseKeyValueDefine.RETURN_PACKAGE_SUCCESS_MESSAGE);

        String result = jsonMapResult.toString();
        return result;
    }
*/
}
